package com.anosine.gpsl.android.impl;

import com.anosine.gpsl.android.AppConstants;
import com.anosine.gpsl.android.WebConstants;
import com.anosine.gpsl.android.dao.InitPwdDao;
import com.anosine.gpsl.android.entity.Login;
import com.anosine.gpsl.android.entity.ModifyPwdInfo;
import com.anosine.gpsl.android.presenter.BasePresenterActivity.OnLoadListener;
import com.anosine.gpsl.android.util.HttpRequestListener;
import com.anosine.gpsl.android.util.SendHttpUtils;
import com.anosine.gpsl.android.util.ToastHelper;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;

/**
 * @Title: InitPwdDaoImpl
 * @Package com.anosine.gpsl.android.impl
 * @Description: 接口实现
 * @author: sunny
 * @date: 2015-11-19
 * @version V1.0
 */
public class InitPwdDaoImpl implements InitPwdDao {
	private OnLoadListener loadListener;

	/**
	 * @Title: InitPwdDaoImpl(OnLoadListener loadListener)
	 * @Description: 构造方法，初始化对象，给loadListener赋值
	 * @param: loadListener
	 * @throws
	 */

	public InitPwdDaoImpl(OnLoadListener loadListener) {
		super();
		this.loadListener = loadListener;
	}

	/**
	 * @Title: getInitPwd(String usermd5, String oldpassword,String newpassword)
	 * @Description: 调用初始化密码接口
	 * @param: usermd5 登录验证串
	 * @param: oldpassword 旧密码
	 * @param: newpassword 新密码
	 * @return: 无
	 * @throws
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void getInitPwd(String usermd5, String oldpassword,
			String newpassword) {

		RequestParams params = new RequestParams();
		params.addBodyParameter("usermd5", usermd5);
		params.addBodyParameter("oldpassword", oldpassword);
		params.addBodyParameter("newpassword", newpassword);

		SendHttpUtils sendHttp = new SendHttpUtils();
		TypeToken type = new TypeToken<ModifyPwdInfo>() {
		};
		String url = String.format("%1s/%2s", AppConstants.sCommonUrl,
				WebConstants.Do_ModifyPassword_Action);
		sendHttp.doRequest(url, params, type,
				new HttpRequestListener<ModifyPwdInfo>() {
					/**
					 * @Title: onSuccess(String result, ModifyPwdInfo t)
					 * @Description: 解析成功做的操作
					 * @param: result json
					 * @param: t 修改密码的实体类
					 * @return: 无
					 * @throws
					 */
					@Override
					public void onSuccess(String result, ModifyPwdInfo t) {

						loadListener.onSuccess(t);

					}
					/**
					 * @Title: onFailure(String error)
					 * @Description: 解析失败做的操作
					 * @param: error 错误信息
					 * @return: 无
					 * @throws
					 */
					@Override
					public void onFailure(String error) {
						loadListener.onFailure();
						ToastHelper.toast(error);
					}
				});
	}

}
